#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
int sql_init(MYSQL *conn)
{
    const char* server="localhost";
    const char* user="root";
    const char* password="root";
    const char* database="netdisc";//要访问的数据库名称
    if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
    {
        printf("Error connecting to database:%s\n",mysql_error(conn));
        return -1;
    }else{
        printf("Connected...\n");
    }
    return 0;
}
int sql_query(MYSQL *conn,MYSQL_RES **res,char *query,unsigned int *num)
{//这里res必须要传递二级指针
    *num=mysql_query(conn,query);
    printf("*num=%u\n",*num);
    puts(query);
    if(*num)
    {
        printf("Error making query:%s\n",mysql_error(conn));
        return -1;
    }else{
        //	printf("Query made...\n");
        *res=mysql_use_result(conn);
    }
    return 0;
}
int sql_insert(MYSQL *conn,char *query)
{
    int num;
    num=mysql_query(conn,query);
    if(num)
    {
        printf("Error making query:%s\n",mysql_error(conn));
        return -1;
    }else{
        printf("insert success\n");
    }
    return 0;

}
int sql_delete(MYSQL *conn,char *query)
{
    int num;
    num=mysql_query(conn,query);
    if(num)
    {
        printf("Error making query:%s\n",mysql_error(conn));
    }else{
        printf("delete success,delete row=%ld\n",(long)mysql_affected_rows(conn));
    }
}
int sql_update(MYSQL *conn,char *query)
{
    int num;
    num=mysql_query(conn,query);
    if(num)
    {
        printf("Error making query:%s\n",mysql_error(conn));
    }else{
        printf("update success\n");
    }
}
int update(int argc,char* argv[])
{
    if(argc!=2)
    {
        printf("error args\n");
        return -1;
    }
    MYSQL *conn;
    //MYSQL_RES *res;
    //MYSQL_ROW row;
    char query[200]="update Person set LastName='"; 
    sprintf(query,"%s%s%s",query,argv[1],"' where FirstName='xi'");
    puts(query);
    conn=mysql_init(NULL);
    sql_init(conn);
    sql_update(conn,query);
    mysql_close(conn);
    return 0;
}
int deleteio(int argc,char* argv[])
{
    MYSQL *conn;
    char query[200]="delete from Person where FirstName='guang'";
    conn=mysql_init(NULL);
    sql_init(conn);
    sql_delete(conn,query);
    mysql_close(conn);
    return 0;
}
int insert(int argc,char* argv[])
{
    MYSQL *conn;
    // MYSQL_RES *res;
    // MYSQL_ROW row;
    char query[200]="insert into Person(FirstName,LastName,Age,salary) values('guang','touqiang',10,2000)";
    conn=mysql_init(NULL);
    sql_init(conn);
    bzero(query,sizeof(query));
    char usr[10]="howl";
    sprintf(query,"insert into repository (code,precode,filename,owner) values (1,0,'%s','%s')",usr,usr);
    printf("query:%s\n",query);
    sql_insert(conn,query);
    mysql_close(conn);
    return 0;
}
int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        printf("error args\n");
        return -1;
    }
    MYSQL *conn;
    conn=mysql_init(NULL);
    if(-1==sql_init(conn)){return -1;}
    MYSQL_RES *res;
    MYSQL_ROW row;
    char query[300]="select size from repository admin where filename='";
    //sprintf(query,"%s%s%s",query, argv[1],"'");
    //int age=atoi(argv[1]);
    sprintf(query,"%s%s'",query,argv[1]);
    //strcpy(query,"select * from Person");
    puts(query);
    unsigned int num;
    sql_query(conn,&res,query,&num);
    printf("res%p\n",res);
    if(res)
    {
        printf("res%p\n",res);
        while((row=mysql_fetch_row(res))!=NULL)
        {	
            printf("row|%p|\n",row);
            printf("strcmp|%d|\n",strcmp("(null)",row[0]));
                printf("|%s||| ",row[0]);
            printf("mysql_num_fields(res)=%d\n",mysql_num_fields(res));
            printf("res%p\n",res);
            //printf("num=%d\n",mysql_num_fields(res));//列数
            for(num=0;num<mysql_num_fields(res);num++)
            {
                printf("|%s||| ",row[num]);
            }
            printf("\n");
        }
    }else{
        printf("Don't find data\n");
    }
    mysql_free_result(res);
    mysql_close(conn);
    return 0;
}
